package com.um.jdy.platform.platform.controller;


import com.um.jdy.common.platform.entity.vo.CompanyUserVO;
import com.um.jdy.platform.platform.entity.dto.UserPageDTO;
import com.um.jdy.platform.platform.entity.dto.UserRolesDTO;
import com.um.jdy.platform.platform.manager.CompanyUserManager;
import com.um.springboot.starter.annotation.ApiPermission;
import com.um.springboot.starter.annotation.RequestJson;
import com.um.springboot.starter.annotation.TokenRequired;
import com.um.springboot.starter.entity.Page;
import com.um.springboot.starter.orm.controller.BaseController;
import com.um.springboot.starter.orm.entity.Response;
import com.um.springboot.starter.orm.params.PageParams;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 控制器：管理员用户表
 */
@RestController
@RequestMapping("/api/v1/cms/platform/user")
@Api(tags = "UserController", description = "管理员用户表")
@ApiPermission("company_user")
@TokenRequired
public class CompanyUserController extends BaseController<CompanyUserManager> {

    /**
     * 查询分页
     * @param params
     * @return
     */
    @ApiPermission("page")
    @ApiOperation("查询分页")
    @PostMapping("/page")
    public Response<Page<CompanyUserVO>> getPage(@RequestBody UserPageDTO params){
        PageParams pageParams = params.toPageParams(new String[]{"name","mobile"},new String[]{"name","mobile"});
        return manager.getPage(pageParams);
    }


    /**
     * 读取用户角色列表
     * @return
     */
    @ApiPermission("role")
    @ApiOperation(value = "读取用户角色列表")
    @GetMapping("/user_role")
    public Response getUserRoles(@RequestParam String userId){
        return manager.getUserRoles(userId);
    }

    /**
     * 新增用户及角色列表
     * @return
     */
    @ApiPermission("add")
    @ApiOperation(value = "新增用户及角色列表")
    @PostMapping
    public Response add(@RequestBody UserRolesDTO userRolesDTO){
        return manager.addEntity(userRolesDTO.getUser(),userRolesDTO.getRoles());
    }

    /**
     * 修改用户及角色列表
     * @return
     */
    @ApiPermission("update")
    @ApiOperation(value = "修改用户及角色列表")
    @PutMapping("{id}")
    public Response update(@PathVariable String id, @RequestBody UserRolesDTO userRolesDTO){
        return manager.updateEntity(id,userRolesDTO.getUser(),userRolesDTO.getRoles());
    }

    /**
     * 重置密码
     * @return
     */
    @ApiPermission("reset")
    @ApiOperation(value = "重置密码")
    @PutMapping("/{userId}/reset_password")
    public Response resetPassword(@PathVariable String userId, @RequestJson String password){
        return manager.resetPassword(userId,password);
    }

    @ApiPermission("delete")
    @ApiOperation("删除")
    @DeleteMapping({"{id}"})
    public Response delete(@PathVariable String id) {
        return manager.delete(id);
    }

    @ApiPermission("delete_all")
    @ApiOperation("批量删除")
    @DeleteMapping
    public Response deleteIds(@ApiParam(value = "删除的id",required = true) @RequestBody List<String> ids) {
        return manager.deleteIds(ids);
    }

    @ApiPermission("enable")
    @ApiOperation("启用、禁用")
    @PutMapping({"{id}/enable"})
    public Response<Object> enable(@PathVariable String id, @RequestParam String enable) {
        return manager.enable(id, enable);
    }

}
